/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.sys.service.impl;

import com.easy.common.util.Utility;
import com.easy.common.utils.CacheUtils;
import com.easy.common.utils.StringUtils;
import com.easy.core.persistence.Page;
import com.easy.core.service.impl.BaseServiceImpl;
import com.easy.modules.sys.entity.Dict;
import com.easy.modules.sys.entity.DictType;
import com.easy.modules.sys.mapper.DictMapper;
import com.easy.modules.sys.mapper.DictTypeMapper;
import com.easy.modules.sys.service.DictTypeService;
import com.easy.modules.sys.utils.DictUtils;
import com.easy.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 数据字典Service
 * @author lgf
 * @version 2017-01-16
 */
@Service
@Transactional(readOnly = true)
public class DictTypeServiceImpl extends BaseServiceImpl implements DictTypeService {

	/**
	 * 持久层对象
	 */
	@Autowired
	private DictTypeMapper mapper;

	@Autowired
	private DictMapper dictMapper;

	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	public DictType get(String id) {
		DictType dictType = mapper.get(id);
		dictType.setDictList(dictMapper.findList(new Dict(dictType)));
		return dictType;
	}

	/**
	 * 获取单条数据
	 * @param typeId
	 * @return
	 */
	public DictType getByTypeId(Integer typeId) {
		DictType dictType = mapper.getByTypeId(typeId);
		dictType.setDictList(dictMapper.findList(new Dict(dictType)));
		return dictType;
	}

	/**
	 * 获取单条Dict数据
	 * @param id
	 * @return
	 */
	public Dict getDict(String id) {
		return dictMapper.get(id);
	}

	/**
	 * 获取单条Dict数据
	 * @param dictId
	 * @return
	 */
	public Dict getDictByDictId(Integer dictId) {
		return dictMapper.getByDictId(dictId);
	}

	/**
	 * 获取单条数据
	 * @param dictType
	 * @return
	 */
	public DictType get(DictType dictType) {
		return mapper.get(dictType);
	}

	/**
	 * 查询列表数据
	 * @param dictType
	 * @return
	 */
	public List<DictType> findList(DictType dictType) {
		dataRuleFilter(dictType);
		return mapper.findList(dictType);
	}

	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param dictType
	 * @return
	 */
	public Page<DictType> findPage(Page<DictType> page, DictType dictType) {
		dataRuleFilter(dictType);
		dictType.setPage(page);
		page.setList(mapper.findList(dictType));
		return page;
	}

	/**
	 * 保存类型数据（插入或更新）
	 * @param dictType
	 */
	@Transactional(readOnly = false)
	public void save(DictType dictType) {
		if (Utility.isNullorEmpty(dictType.getId()) || dictType.getTypeId()<1){
			dictType.preInsert();
			mapper.insert(dictType);
		}else{
			dictType.preUpdate();
			mapper.update(dictType);
		}
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}
	/**
	 * 保存字典值数据（插入或更新）
	 * @param dict
	 */
	@Transactional(readOnly = false)
	public void saveDict(Dict dict) {
		if (Utility.isNullorEmpty(dict.getId()) || dict.getDictId()<1){
			dict.preInsert();
			dictMapper.insert(dict);
		}else{
			dict.preUpdate();
			dictMapper.update(dict);
		}
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}


	/**
	 * 删除字典类型数据
	 * @param dictType
	 */
	@Transactional(readOnly = false)
	public void delete(DictType dictType) {
		//解除数据权限角色关联
		mapper.delete(dictType);
		dictMapper.delete(new Dict(dictType));
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}


	/**
	 * 删除字典值数据
	 * @param dict
	 */
	@Transactional(readOnly = false)
	public void deleteDict(Dict dict) {
		dictMapper.delete(dict);
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}

	/**
	 * 删除全部数据
	 * @param dictTypes
	 */
	@Transactional(readOnly = false)
	public void deleteAll(Collection<DictType> dictTypes) {
		for (DictType dictType : dictTypes) {
			mapper.delete(dictType);
			dictMapper.delete(new Dict(dictType));
		}
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}

	/**
	 * 删除全部数据
	 * @param dictTypes
	 */
	@Transactional(readOnly = false)
	public void deleteAllByLogic(Collection<DictType> dictTypes) {
		for (DictType dictType : dictTypes) {
			mapper.deleteLogic(dictType);
			dictMapper.delete(new Dict(dictType));
		}
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}


	/**
	 * 获取单条数据
	 * @param propertyName, value
	 * @return
	 */
	public DictType findUniqueByProperty(String propertyName, Object value){
		return mapper.findUniqueByProperty(propertyName, value);
	}

	/**
	 * 动态sql
	 * @param sql
	 */

	public List<Object> executeSelectSql(String sql){
		return mapper.execSelectSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeInsertSql(String sql){
		mapper.execInsertSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeUpdateSql(String sql){
		mapper.execUpdateSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeDeleteSql(String sql){
		mapper.execDeleteSql(sql);
	}



}